package com.ishell.generate.pattern.strategy;

import com.ishell.generate.TableDesc;
import com.ishell.generate.util.JdbcUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: crazier.huang
 * @project: generate
 * @date: 2018/7/12
 * @desc: oracle生成表策略
 */
public class GenTableOracleStrategy implements GenTableStrategy {

    private final static Logger logger = LoggerFactory.getLogger(GenTableOracleStrategy.class);


    // 获取表
    String tableSql = "select table_name from user_tables where 1=1";
    String commentSql = "select comments from user_tab_comments where table_name=? ";


    @Override
    public List<TableDesc> genTable() {
        List<TableDesc> tableDescList = new ArrayList<>();

        try {
            JdbcUtil jdbcUtil = new JdbcUtil();
            jdbcUtil.getConnection();
            if(null!=excludeList){
                tableSql = tableSql +" and table_name not in ( ? ) ";
            }
            if(null!=includeList ){
                tableSql = tableSql +" and table_name in ( ? ) ";
            }

            ResultSet set = jdbcUtil.findResultSet(tableSql,excludeTable,includeTable);
            while (set.next()){
                TableDesc tableDesc = new TableDesc(set.getString(1));
                ResultSet commentSet = jdbcUtil.findResultSet(commentSql,tableDesc.getName());
                while (commentSet.next()){
                    tableDesc.setComment(commentSet.getString(1));
                }

                tableDescList.add(tableDesc);

            }

        } catch (SQLException e) {
            logger.error("执行脚本报错",e);
            return null;

        }



        return tableDescList;
    }
}
